using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0712518_0712510_LTGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    /// 
  

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteManager spriteManager;
        VisibleGameObject m_VisibleGameObject;
        VisibleGameEntity m_VisibleGameEntity;

        Texture2D backgroundMenu;
        Menu menu;
        Title title;
        int m_nIsFinish = 0;

        CharacterManager _characterManager;
        MapManager _mapManager;

        Vector2 pos1 = new Vector2();
        Vector2 position = new Vector2(325,300);
        Vector2 pos2 = new Vector2();
        int timeSinceLastFrame = 0;
        int millisecondsPerFrame = 40;
        Point frameSize = new Point(50, 50);
        Point currentFrame = new Point(14, 0);
        Map m_Map1;
        int onGround = 0;
        int input = 0;
        Point sheetSize = new Point(16, 1);

        KeyboardState m_Keyboard;
        int m_nStatusGame = 0;
        Texture2D m_Texture2D;

        Sprite[] run;



        MapManager map1;



        public Game1()
        {            
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
          //  graphics.IsFullScreen = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            spriteManager = new SpriteManager(this);
            m_VisibleGameObject = new VisibleGameObject();
            m_VisibleGameEntity = new VisibleGameEntity();
            
            _mapManager = new MapManager();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            m_VisibleGameObject._nsprite = 16;
            m_VisibleGameObject._sprite = new MySprite[m_VisibleGameObject._nsprite];
            m_Texture2D = Content.Load<Texture2D>(@"imagesZero/zerorun_edit");
            //load background menu game
            backgroundMenu = Content.Load<Texture2D>(@"imagesMenu/background2");
            
            ////load title game            
            title = new Title();
            title.LoadContent(this.Content);
            
            ////load menu game                            
            menu = new Menu();
            menu.LoadContent(this.Content);

            //Khi click vao nut Start thi load man 1 len
            //m_Map1 = new Map();
            //m_Map1.LoadContent(this.Content);

            map1 = new MapManager();
            map1.LoadContent(Content);
          /*  run = new Sprite[16];

            run[0] = new Sprite();
            run[0].start_position.X = 25;
            run[0].start_position.Y = 50;
            run[0].size.X = 50;
            run[0].size.Y = 50;
            run[0].position2draw.X = -25;
            run[0].position2draw.Y = -50;
            run[0].conclision.X = -17;
            run[0].conclision.Y = -43;
            run[0].conclisionSize.X = 30;
            run[0].conclisionSize.Y = 43;
 

            run[1] = new Sprite();
            run[1].start_position.X = 76;
            run[1].start_position.Y = 49;
            run[1].size.X = 50;
            run[1].size.Y = 50;
            run[1].position2draw.X = -25;
            run[1].position2draw.Y = -49;
            run[1].conclision.X = -17;
            run[1].conclision.Y = -42;
            run[1].conclisionSize.X = 31;
            run[1].conclisionSize.Y = 42;

            run[2] = new Sprite();
            run[2].start_position.X =127;
            run[2].start_position.Y = 50;
            run[2].size.X = 50;
            run[2].size.Y = 50;
            run[2].position2draw.X = -25;
            run[2].position2draw.Y = -50;
            run[2].conclision.X = -16;
            run[2].conclision.Y = -45;
            run[2].conclisionSize.X = 30;
            run[2].conclisionSize.Y = 45;

            run[3] = new Sprite();
            run[3].start_position.X =178;
            run[3].start_position.Y = 50;
            run[3].size.X = 50;
            run[3].size.Y = 50;
            run[3].position2draw.X = -25;
            run[3].position2draw.Y = -50;
            run[3].conclision.X = -9;
            run[3].conclision.Y = -45;
            run[3].conclisionSize.X = 26;
            run[3].conclisionSize.Y = 45;

            run[4] = new Sprite();
            run[4].start_position.X =229;
            run[4].start_position.Y = 50;
            run[4].size.X = 50;
            run[4].size.Y = 50;
            run[4].position2draw.X = -25;
            run[4].position2draw.Y = -50;
            run[4].conclision.X = -7;
            run[4].conclision.Y = -45;
            run[4].conclisionSize.X = 23;
            run[4].conclisionSize.Y = 45;

            run[5] = new Sprite();
            run[5].start_position.X =280;
            run[5].start_position.Y = 50;
            run[5].size.X = 50;
            run[5].size.Y = 50;
            run[5].position2draw.X = -25;
            run[5].position2draw.Y = -50;
            run[5].conclision.X = -7;
            run[5].conclision.Y = -45;
            run[5].conclisionSize.X = 23;
            run[5].conclisionSize.Y = 45;

            run[6] = new Sprite();
            run[6].start_position.X =331;
            run[6].start_position.Y = 50;
            run[6].size.X = 50;
            run[6].size.Y = 50;
            run[6].position2draw.X = -25;
            run[6].position2draw.Y = -50;
            run[6].conclision.X = -12;
            run[6].conclision.Y = -44;
            run[6].conclisionSize.X = 29;
            run[6].conclisionSize.Y = 44;

            run[7] = new Sprite();
            run[7].start_position.X =382;
            run[7].start_position.Y = 50;
            run[7].size.X = 50;
            run[7].size.Y = 50;
            run[7].position2draw.X = -25;
            run[7].position2draw.Y = -50;
            run[7].conclision.X = -15;
            run[7].conclision.Y = -44;
            run[7].conclisionSize.X = 33;
            run[7].conclisionSize.Y = 44;

            run[8] = new Sprite();
            run[8].start_position.X =433;
            run[8].start_position.Y = 50;
            run[8].size.X = 50;
            run[8].size.Y = 50;
            run[8].position2draw.X = -25;
            run[8].position2draw.Y = -50;
            run[8].conclision.X = -18;
            run[8].conclision.Y = -44;
            run[8].conclisionSize.X = 36;
            run[8].conclisionSize.Y = 44;

            run[9] = new Sprite();
            run[9].start_position.X =484;
            run[9].start_position.Y = 50;
            run[9].size.X = 50;
            run[9].size.Y = 50;
            run[9].position2draw.X = -25;
            run[9].position2draw.Y = -50;
            run[9].conclision.X = -11;
            run[9].conclision.Y = -45;
            run[9].conclisionSize.X = 26;
            run[9].conclisionSize.Y = 45;

            run[10] = new Sprite();
            run[10].start_position.X =535;
            run[10].start_position.Y = 50;
            run[10].size.X = 50;
            run[10].size.Y = 50;
            run[10].position2draw.X = -25;
            run[10].position2draw.Y = -50;
            run[10].conclision.X = -8;
            run[10].conclision.Y = -47;
            run[10].conclisionSize.X = 24;
            run[10].conclisionSize.Y = 47;

            run[11] = new Sprite();
            run[11].start_position.X =586;
            run[11].start_position.Y = 50;
            run[11].size.X = 50;
            run[11].size.Y = 50;
            run[11].position2draw.X = -25;
            run[11].position2draw.Y = -50;
            run[11].conclision.X = -10;
            run[11].conclision.Y = -47;
            run[11].conclisionSize.X = 27;
            run[11].conclisionSize.Y = 47;

            run[12] = new Sprite();
            run[12].start_position.X =637;
            run[12].start_position.Y = 50;
            run[12].size.X = 50;
            run[12].size.Y = 50;
            run[12].position2draw.X = -25;
            run[12].position2draw.Y = -50;
            run[12].conclision.X = -14;
            run[12].conclision.Y = -47;
            run[12].conclisionSize.X = 28;
            run[12].conclisionSize.Y = 47;

            run[13] = new Sprite();
            run[13].start_position.X =688;
            run[13].start_position.Y = 50;
            run[13].size.X = 50;
            run[13].size.Y = 50;
            run[13].position2draw.X = -25;
            run[13].position2draw.Y = -50;
            run[13].conclision.X = -18;
            run[13].conclision.Y = -47;
            run[13].conclisionSize.X = 36;
            run[13].conclisionSize.Y = 47;

            run[14] = new Sprite();
            run[14].start_position.X =739;
            run[14].start_position.Y = 50;
            run[14].size.X = 50;
            run[14].size.Y = 50;
            run[14].position2draw.X = -25;
            run[14].position2draw.Y = -50;
            run[14].conclision.X = -22;
            run[14].conclision.Y = -46;
            run[14].conclisionSize.X = 39;
            run[14].conclisionSize.Y = 46;

            run[15] = new Sprite();
            run[15].start_position.X =790;
            run[15].start_position.Y = 50;
            run[15].size.X = 50;
            run[15].size.Y = 50;
            run[15].position2draw.X = -25;
            run[15].position2draw.Y = -50;
            run[15].conclision.X = -20;
            run[15].conclision.Y = -45;
            run[15].conclisionSize.X = 36;
            run[15].conclisionSize.Y = 45; */
            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            if (m_nIsFinish == 0)
                m_nIsFinish = title.Update(gameTime);
            if (m_nIsFinish == 1)
                menu.Update(gameTime);
            m_Keyboard = Keyboard.GetState();
            map1.Update(gameTime, Window.ClientBounds.Width, Window.ClientBounds.Height);
            //this.UpdateInput(gameTime);
            //this.UpdateMovement(gameTime);
              //checkToMap();
            //centerEntityOnMap();
           // m_Map1.Update(gameTime, Window.ClientBounds.Width, Window.ClientBounds.Height);
            base.Update(gameTime);
        }

        //rectangle
        //void checkToMap()
        //{
        //    int i, x1, x2, y1, y2;

        //    /* Remove the user from the ground */

        //   // onGround = 0;
        //    pos1.X = position.X + run[currentFrame.X].conclision.X;
        //    if (huong == 0)
        //    {
        //        pos1.X = position.X - (run[currentFrame.X].conclisionSize.X + run[currentFrame.X].conclision.X);
        //    }
        //    pos1.Y = position.Y + run[currentFrame.X].conclision.Y;
        //    /* Test the horizontal movement first */

        //    i = run[currentFrame.X].conclisionSize.Y > m_Map1.tile_size ? m_Map1.tile_size : run[currentFrame.X].conclisionSize.Y;

        //    for (; ; )
        //    {
                
        //        x1 = (int)(pos1.X + pos2.X) / m_Map1.tile_size;
        //        x2 = (int)(pos1.X + pos2.X + run[currentFrame.X].conclisionSize.X - 1) / m_Map1.tile_size;

               
        //        y1 = (int)(pos1.Y) / m_Map1.tile_size;
        //        y2 = (int)(pos1.Y + i - 1) / m_Map1.tile_size;

        //        if (x1 >= 0 && x2 < 37 && y1 >= 0 && y2 < 14)
        //        {
        //            if (pos2.X > 0)
        //            {
        //                /* Trying to move right */

        //                if ((m_Map1.tile[y1, x2] != 0) || (m_Map1.tile[y2, x2] != 0))
        //                {
        //                    /* Place the player as close to the solid tile as possible */

        //                    pos1.X = x2 * m_Map1.tile_size;

        //                    pos1.X -= run[currentFrame.X].conclisionSize.X; //+ 1;

        //                    pos2.X = 0;
        //                }
        //            }

        //            else if (pos2.X < 0)
        //            {
        //                /* Trying to move left */

        //                if ((m_Map1.tile[y1, x1] != 0) || (m_Map1.tile[y2, x1] != 0))
        //                {
        //                    /* Place the player as close to the solid tile as possible */

        //                    pos1.X = (x1 + 1) * m_Map1.tile_size; //+1;

        //                    pos2.X = 0;
        //                }
        //            }
        //        }

        //        if (i == run[currentFrame.X].conclisionSize.Y)
        //        {
        //            break;
        //        }

        //        i += m_Map1.tile_size;

        //        if (i > run[currentFrame.X].conclisionSize.Y)
        //        {
        //            i = run[currentFrame.X].conclisionSize.Y;
        //        }
        //    }

        //    /* Now test the vertical movement */

        //    i = run[currentFrame.X].conclisionSize.X > m_Map1.tile_size ? m_Map1.tile_size : run[currentFrame.X].conclisionSize.X;

        //    for (; ; )
        //    {
                
        //        x1 = (int)(pos1.X) / m_Map1.tile_size;
        //        x2 = (int)(pos1.X + i -1) / m_Map1.tile_size;

        //        y1 = (int)(pos1.Y + pos2.Y) / m_Map1.tile_size;
        //        y2 = (int)(pos1.Y + pos2.Y + run[currentFrame.X].conclisionSize.Y) / m_Map1.tile_size;

        //        if (x1 >= 0 && x2 < 37 && y1 >= 0 && y2 < 14)
        //        {
        //            if (pos2.Y > 0)
        //            {
        //                /* Trying to move down */
        //                onGround = 0;
        //                if ((m_Map1.tile[y2, x1] != 0) || (m_Map1.tile[y2, x2] != 0))
        //                {
        //                    /* Place the player as close to the solid tile as possible */

        //                    pos1.Y = y2 * m_Map1.tile_size;
        //                    pos1.Y -= run[currentFrame.X].conclisionSize.Y;

        //                    pos2.Y = 0;
        //                    input = 0;
        //                    onGround = 1;
        //                }
        //            }

        //            else if (pos2.Y < 0)
        //            {
        //                /* Trying to move up */

        //                if ((m_Map1.tile[y1, x1] != 0) || (m_Map1.tile[y1, x2] != 0))
        //                {
        //                    /* Place the player as close to the solid tile as possible */

        //                    pos1.Y = (y1 + 1) * m_Map1.tile_size;

        //                    pos2.Y = 0;
        //                }
        //            }
        //        }

        //        if (i == run[currentFrame.X].conclisionSize.X)
        //        {
        //            break;
        //        }

        //        i += m_Map1.tile_size;

        //        if (i > run[currentFrame.X].conclisionSize.X)
        //        {
        //            i = run[currentFrame.X].conclisionSize.X;
        //        }
        //    }

        //    /* Now apply the movement */
            
        //    pos1.X += pos2.X;
        //    pos1.Y += pos2.Y;
        //    position.X = pos1.X - run[currentFrame.X].conclision.X;
        //    //if (huong == 1)
        //    //{
        //    //    position.X = pos1.X - run[currentFrame.X].conclision.X;
        //    //}
        //    if (huong == 0)
        //    {
        //        position.X = pos1.X +(run[currentFrame.X].conclisionSize.X + run[currentFrame.X].conclision.X);
        //    }
        //    position.Y = pos1.Y-run[currentFrame.X].conclision.Y;

        //    if (pos1.X < 0)
        //    {
        //        pos1.X = 0;
        //        position.X = pos1.X + (run[currentFrame.X].conclisionSize.X + run[currentFrame.X].conclision.X); //+ 1;
        //    }

        //    else if (pos1.X + run[currentFrame.X].conclisionSize.X >= m_Map1.MaxX)
        //    {
        //        pos1.X = m_Map1.MaxX - run[currentFrame.X].conclisionSize.X; //- 1;
        //        position.X = pos1.X - run[currentFrame.X].conclision.X;
        //    }

        //    if (pos1.Y > m_Map1.MaxY)
        //    {
        //        pos1.Y = pos1.X = pos2.X = pos2.Y = 0;
        //        position.X = pos1.X - run[currentFrame.X].conclision.X;
        //        position.Y = pos1.Y - run[currentFrame.X].conclision.Y;
        //    }
        //}
        //position
        //void checkToMap()
        //{
        //    int i, x1, x2, y1, y2;

        //    /* Remove the user from the ground */

        //    onGround = 0;

        //    /* Test the horizontal movement first */

        //    i = run[currentFrame.X].position2draw.Y > m_Map1.tile_size ? m_Map1.tile_size : run[currentFrame.X].position2draw.Y;
        //    //pos1.X = run[currentFrame.X].position.X - run[currentFrame.X].position2draw.X;
        //    //pos1.Y = run[currentFrame.X].position.Y - run[currentFrame.X].position2draw.Y;
        //    for (; ; )
        //    {


        //        x1 = (int)((position.X - run[currentFrame.X].position2draw.X) + pos2.X) / m_Map1.tile_size;
        //        x2 = (int)(position.X + pos2.X + (run[currentFrame.X].size.X - run[currentFrame.X].position2draw.X) ) / m_Map1.tile_size;

        //        y2 = (int)(position.Y - 10) / m_Map1.tile_size;  //lun lun phai - 1 
        //        y1 = (int)(position.Y - i + 20 ) / m_Map1.tile_size;

        //        if (x1 >= 0 && x2 < 37 && y1 >= 0 && y2 < 14)
        //        {
        //            if (pos2.X > 0)
        //            {
        //                /* Trying to move right */

        //                if ((m_Map1.tile[y1, x2] != 0) || (m_Map1.tile[y2, x2] != 0))
        //                {
        //                    /* Place the player as close to the solid tile as possible */

        //                    position.X = x2 * m_Map1.tile_size;

        //                    position.X -= (run[currentFrame.X].size.X - run[currentFrame.X].position2draw.X);

        //                    pos2.X = 0;
        //                }
        //            }

        //            else if (pos2.X < 0)
        //            {
        //                /* Trying to move left */

        //                if ((m_Map1.tile[y1, x1] != 0) || (m_Map1.tile[y2, x1] != 0))
        //                {
        //                    /* Place the player as close to the solid tile as possible */

        //                    position.X = (x1 + 1) * m_Map1.tile_size;
        //                    position.X += run[currentFrame.X].position2draw.X;

        //                    pos2.X = 0;
        //                }
        //            }
        //        }

        //        if (i == run[currentFrame.X].position2draw.Y)
        //        {
        //            break;
        //        }

        //        i += m_Map1.tile_size;

        //        if (i > run[currentFrame.X].position2draw.Y)
        //        {
        //            i = run[currentFrame.X].position2draw.Y;
        //        }
        //    }

        //    /* Now test the vertical movement */

        //    i = run[currentFrame.X].size.X > m_Map1.tile_size ? m_Map1.tile_size : run[currentFrame.X].size.X;

        //    for (; ; )
        //    {
        //        x1 = (int)(position.X - run[currentFrame.X].position2draw.X + 10 ) / m_Map1.tile_size;
        //        x2 = (int)( position.X + (i- run[currentFrame.X].position2draw.X) - 10 ) / m_Map1.tile_size; //lun lun phai - 1

        //        y1 = (int)(position.Y + pos2.Y) / m_Map1.tile_size;
        //        y2 = (int)(position.Y + pos2.Y - run[currentFrame.X].position2draw.Y) / m_Map1.tile_size;

        //        if (x1 >= 0 && x2 < 37 && y2 >= 0 && y1 < 14)
        //        {
        //            if (pos2.Y > 0)
        //            {
        //                /* Trying to move down */

        //                if ((m_Map1.tile[y1, x1] != 0) || (m_Map1.tile[y1, x2] != 0))
        //                {
        //                    /* Place the player as close to the solid tile as possible */

        //                   position.Y = y1 * m_Map1.tile_size;
        //                    //run[currentFrame.X].position.Y -= run[currentFrame.X].position2draw.Y;

        //                    pos2.Y = 0;
        //                    input = 0;
        //                    onGround = 1;
        //                }
        //            }

        //            else if (pos2.Y < 0)
        //            {
        //                /* Trying to move up */

        //                if ((m_Map1.tile[y2, x1] != 0) || (m_Map1.tile[y2, x2] != 0))
        //                {
        //                    /* Place the player as close to the solid tile as possible */

        //                    position.Y = ((y2 + 1) * m_Map1.tile_size) + run[currentFrame.X].position2draw.Y ;

        //                    pos2.Y = 0;
        //                }
        //            }
        //        }

        //        if (i == run[currentFrame.X].size.X)
        //        {
        //            break;
        //        }

        //        i += m_Map1.tile_size;

        //        if (i > run[currentFrame.X].size.X)
        //        {
        //            i = run[currentFrame.X].size.X;
        //        }
        //    }

        //    /* Now apply the movement */

        //    position.X += pos2.X;
        //    position.Y += pos2.Y;

        //    if (position.X - run[currentFrame.X].position2draw.X < 0)
        //    {
        //        position.X = run[currentFrame.X].position2draw.X;
        //    }

        //    else if (position.X + (run[currentFrame.X].size.X - run[currentFrame.X].position2draw.X) >= m_Map1.MaxX)
        //    {
        //        position.X = m_Map1.MaxX - (run[currentFrame.X].size.X - run[currentFrame.X].position2draw.X) - 1;
        //    }

        //    if (position.Y> m_Map1.MaxY)
        //    {
        //        position.X = 25;
        //        position.Y = 50;
        //        pos2.X = pos2.Y = 0;
        //    }
        //}
        //void centerEntityOnMap()
        //{
        //    m_Map1.StartPosition.X = pos1.X - (Window.ClientBounds.Width / 2);

        //    if (m_Map1.StartPosition.X < 0)
        //    {
        //        m_Map1.StartPosition.X = 0;
        //    }

        //    else if (m_Map1.StartPosition.X + Window.ClientBounds.Width >= m_Map1.MaxX)
        //    {
        //        m_Map1.StartPosition.X = m_Map1.MaxX - Window.ClientBounds.Width;
        //    }

        //    m_Map1.StartPosition.Y = pos1.Y - (Window.ClientBounds.Height / 2);

        //    if (m_Map1.StartPosition.Y < 0)
        //    {
        //        m_Map1.StartPosition.Y = 0;
        //    }

        //    else if (m_Map1.StartPosition.Y + Window.ClientBounds.Height >= m_Map1.MaxY)
        //    {
        //        m_Map1.StartPosition.Y = m_Map1.MaxY - Window.ClientBounds.Height;
        //    }
        //}
        void centerEntityOnMap(Hero zero)
        {
            m_Map1.StartPosition.X = zero.position.X - (Window.ClientBounds.Width / 2);

            if (m_Map1.StartPosition.X < 0)
            {
                m_Map1.StartPosition.X = 0;
            }

            else if (m_Map1.StartPosition.X + Window.ClientBounds.Width >= m_Map1.MaxX)
            {
                m_Map1.StartPosition.X = m_Map1.MaxX - Window.ClientBounds.Width;
            }

            m_Map1.StartPosition.Y = zero.position.Y - (Window.ClientBounds.Height / 2);

            if (m_Map1.StartPosition.Y < 0)
            {
                m_Map1.StartPosition.Y = 0;
            }

            else if (m_Map1.StartPosition.Y + Window.ClientBounds.Height >= m_Map1.MaxY)
            {
                m_Map1.StartPosition.Y = m_Map1.MaxY - Window.ClientBounds.Height;
            }
        }
        int huong = 1;
        private void UpdateMovement(GameTime gameTime)
        {
          
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > millisecondsPerFrame)
            //{
            //    timeSinceLastFrame -= millisecondsPerFrame;
            //    ++currentFrame.X;
            //    if (currentFrame.X >= sheetSize.X)
            //    {
            //        currentFrame.X = 2;
            //        ++currentFrame.Y;
            //        if (currentFrame.Y >= sheetSize.Y)
            //            currentFrame.Y = 0;
            //    }
            //}
            {
                timeSinceLastFrame -= millisecondsPerFrame;
                pos2.Y += 0.8f;
                pos2.X *= 0.6f;
                if (m_Keyboard.GetPressedKeys().Length == 0)
                {
                    currentFrame.X = 0;
                }
                if (m_Keyboard.IsKeyDown(Keys.Right))
                {
                    huong = 1;
                    pos2.X += 2;
                    ++currentFrame.X;
                    if (currentFrame.X >= sheetSize.X)
                    {
                        currentFrame.X = 2;
                        ++currentFrame.Y;
                        if (currentFrame.Y >= sheetSize.Y)
                            currentFrame.Y = 0;
                    }
                }
                if (m_Keyboard.IsKeyDown(Keys.Left))
                {
                    pos2.X -= 2;
                    ++currentFrame.X;
                    huong = 0;
                    if (currentFrame.X >= sheetSize.X)
                    {
                        currentFrame.X = 2;
                        ++currentFrame.Y;
                        if (currentFrame.Y >= sheetSize.Y)
                            currentFrame.Y = 0;
                    }
                }
                if (m_Keyboard.IsKeyDown(Keys.Up))
                {
                    //onGround = 0;
                    if (input == 0 && onGround==1)
                    {
                        pos2.Y = -8;
                        onGround = 0;
                        input = 1;
                        currentFrame.X = 0;
                    }
                  
                }
            }
            
        }
        private void UpdateInput(GameTime gameTime)
        {            
            
            if (m_Keyboard.IsKeyDown(Keys.Enter))
            {
                if (menu.type == 0)
                {
                    m_nStatusGame = 1;
                }
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            //if (m_nStatusGame == 0)
            //{
            //    spriteBatch.Draw(backgroundMenu, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White);
            //    title.Draw(this.spriteBatch);
            //    menu.Draw(this.spriteBatch);
            //}
            

            //COI LAI CHO NAY//////////////////////////////////////////////
            //if (m_nStatusGame == 1)
            
            //{
                //if (huong == 1)
                //{
                ////    spriteBatch.Draw(m_Texture2D, new Vector2(pos1.X - m_Map1.StartPosition.X, pos1.Y - m_Map1.StartPosition.Y),
                ////new Rectangle((currentFrame.X * frameSize.X) + currentFrame.X, 1, frameSize.X, frameSize.Y),
                ////Color.White, 0, Vector2.Zero,
                ////1, SpriteEffects.None, 0);
                //    spriteBatch.Draw(m_Texture2D, new Vector2((position.X + run[currentFrame.X].position2draw.X) - m_Map1.StartPosition.X, (position.Y + run[currentFrame.X].position2draw.Y) - m_Map1.StartPosition.Y),
                //new Rectangle((int)(run[currentFrame.X].start_position.X + run[currentFrame.X].position2draw.X), (int)(run[currentFrame.X].start_position.Y + run[currentFrame.X].position2draw.Y), frameSize.X, frameSize.Y),
                //Color.White, 0, Vector2.Zero,
                //1, SpriteEffects.None, 0);
                //}
                //if (huong == 0)
                //{
                ////    spriteBatch.Draw(m_Texture2D, new Vector2(pos1.X - m_Map1.StartPosition.X, pos1.Y - m_Map1.StartPosition.Y),
                ////new Rectangle((currentFrame.X * frameSize.X) + currentFrame.X, 1, frameSize.X, frameSize.Y),
                ////Color.White, 0, Vector2.Zero,
                ////1, SpriteEffects.FlipHorizontally, 0);
                //    spriteBatch.Draw(m_Texture2D, new Vector2((position.X + run[currentFrame.X].position2draw.X) - m_Map1.StartPosition.X, (position.Y + run[currentFrame.X].position2draw.Y) - m_Map1.StartPosition.Y),
                //new Rectangle((int)(run[currentFrame.X].start_position.X + run[currentFrame.X].position2draw.X), (int)(run[currentFrame.X].start_position.Y + run[currentFrame.X].position2draw.Y), frameSize.X, frameSize.Y),
                //Color.White, 0, Vector2.Zero,
                //1, SpriteEffects.FlipHorizontally, 0);
                //}

                //m_Map1.Draw(spriteBatch, Window.ClientBounds.Width, Window.ClientBounds.Height);
               
            //}

            map1.Draw(spriteBatch, Window.ClientBounds.Width, Window.ClientBounds.Height);
        
            
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
